<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        class="edit-btn"
        type="danger"
        plain
        round
        size="mini"
        @click="isChanneEdit != isChanneEdit"
        >{{ isChanneEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(item, index) in myChannel"
        :key="item.id"
        @click="myChannelClick(item, index)"
      >
        <van-icon slot="icon" name="clear" v-if="isChanneEdit"></van-icon>
        <span
          class="text"
          slot="text"
          :class="{ active: index === activeIndex }"
          >{{ item.name }}</span
        >
      </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        icon="plus"
        :text="item.name"
        v-for="item in recommendCannel"
        :key="item.id"
        @click="addChannel(item)"
      />
    </van-grid>
    <!-- /频道推荐 -->
  </div>
</template>

<script>
import { getAllChannel, saveMyChannel, deleteMyChannel } from "@/api/channel";
import { setItem } from "@/utils/storage";
import { mapGetters } from "vuex";
export default {
  name: "ChannelEdit",
  components: {},
  props: {
    myChannel: {
      type: Array,
      required: true,
    },
    // 激活高亮下标
    activeIndex: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      allChannel: [],
      isChanneEdit: false,
    };
  },
  computed: {
    recommendCannel() {
      let arr = [];
      this.allChannel.forEach((item) => {
        let row = this.myChannel.find((value) => item.id === value.id);
        if (!row) arr.push(item);
      });
      return arr;
    },
    ...mapGetters(["token"]),
  },
  watch: {},
  created() {
    this.loalChannel();
  },

  methods: {
    // 封装组件

    async loalChannel() {
      let { data } = await getAllChannel();

      console.log(data);
      this.allChannel = data.data.channels;
    },
    //完成添加功能，添加一次重新计算，单向绑定数据流
    async addChannel(val) {
      this.myChannel.push(val);
      if (this.$store.getters.token) {
        try {
          await saveMyChannel({
            channels: [
              {
                id: val.id,
                seq: this.myChannel.length - 1,
              },
            ],
          });
        } catch (error) {
          console.log("error");
          this.$toast.fail("保存失败");
        }
      } else {
        setItem("TOUTIAO_CHANNEL", this.myChannel);
      }
    },

    myChannelClick(val, idx) {
      if (this.isChanneEdit) {
        if (val.name === "推荐") return false;
        this.myChannel.splice(idx, 1);
        if (idx < this.activeIndex) {
          this.$emit("update-active", this.activeIndex - 1);
        }
        this.deleteMyChannel(val);
      } else {
        this.$emit("update-active", idx);
        this.$emit("close");
      }
    },
  },
  // 删除函数
  async deleteSave(item) {
    if (this.token) {
      try {
        await deleteMyChannel(item.id);
      } catch (error) {
        console.log(error);
        this.$toast.fail("删除失败");
      }
    } else {
      setItem("TOUTIAO_CHANNEL", this.myChannel);
    }
  },
};
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #333333;
  }

  .edit-btn {
    width: 150px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }

  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>